<template>
  <div>
    <div class="combox" v-if="Mycommont">
      <div class="container">
        <div class="toplogo">
          <button class="comebreak" @click="$router.go(-1)">
            <div class="left iconfont icon-arrow-left-bold"></div>
          </button>
          <p style="font-weight: bold; font-size: 16px">我的评论 ({{Mycommont.length}})</p>
          <p style="width: 28px"></p>
        </div>
        <div class="comment">
          <div class="usercom">
            <div class="userbox" v-for="(item, index) in Mycommont" :key="index">
              <div class="top">
                <div class="user"  @click="tomy(item)">
                  <div class="userimg">
                    <img :src="item.img" alt="" class="auto-img" />
                  </div>
                  <p style="margin-left: 10px">{{ item.name }}</p>
                </div>
                <div class="right">
                  <div class="num">{{ item.like }}</div>
                  <div class="like" @click="likes(item)">
                    <svg class="icon" aria-hidden="true">
                      <use xlink:href="#icon-fabulous" v-show="item.good"></use>
                      <use xlink:href="#icon-aixin" v-show="!item.good"></use>
                    </svg>
                  </div>
                </div>
              </div>
              <div class="btm" style="margin-left: 43px; font-size: 14px">
                <div class="usercomment" style="margin-top: 5px">
                  {{ item.content }}
                </div>
                <div class="reply" style="margin-top: 10px">
                  <div class="time">{{ item.time }}</div>
                  <div
                    @click="del(item.id)"
                    class="replybox"
                    v-show="item.name == '管理员404' && token"
                  >
                    删除
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
       
      </div>
      
    
    </div>
  </div>
</template>

<script>
import { Dialog } from "vant";
import { getLists } from "../api/lists";
import "../assets/font/iconfont.js";
export default {
  data() {
    return {
        Commont: null,//评论数据
        show:false,//底部评论弹窗
        token:window.localStorage.getItem("token"),
        Mycommont:null,
    };
  },
  methods: {
    getListsFun() {
      getLists().then((data) => {
        this.Commont=data.commontpage;
        console.log(this.Commont);
        this.Mycommont=data.mycommont;
        if (window.localStorage.getItem("Mycommont")) {
          this.Mycommont = JSON.parse(window.localStorage.getItem("Mycommont"))[0];
        }
        if (window.localStorage.getItem("Commont")) {
          this.Commont = JSON.parse(window.localStorage.getItem("Commont"))[0];
        }
      });
      
    },
    //跳转到个人
        tomy(item){
          let token = window.localStorage.getItem("token");
          if(!token){
           return
          }else if(item.name=='管理员404'){

            this.$router.push("/my")
          }
        },
    //评论点赞
    likes(item) {
        item.good = !item.good;
        if (item.good) {
          item.like -= 1;
          window.localStorage.setItem(
            "Commont",
            JSON.stringify([this.Commont])
          );
          window.localStorage.setItem(
              "Mycommont",
              JSON.stringify([this.Mycommont])
            );
        } else {
          item.like += 1;
          window.localStorage.setItem(
            "Commont",
            JSON.stringify([this.Commont])
          );
          window.localStorage.setItem(
              "Mycommont",
              JSON.stringify([this.Mycommont])
            );
        }
      
    },
    //删除评论
    del(id) {
      console.log(id);
      let index = this.Commont.findIndex((item) => item.id == id);
      console.log(index);
      let index1 = this.Mycommont.findIndex((item) => item.id == id);
      console.log(index1);
      console.log(this.Commont);
      
        Dialog.confirm({
          title: "确认要删除当前评论吗?",
        })
          //点击确认删除
          .then(() => {
            console.log(11);
            this.Commont.splice(index, 1);
            this.Mycommont.splice(index1, 1);
            this.$toast.fail("已删除");
            //存入评论区数据并覆盖
            window.localStorage.setItem(
              "Commont",
              JSON.stringify([this.Commont])
            );
            window.localStorage.setItem(
              "Mycommont",
              JSON.stringify([this.Mycommont])
            );
          }) 
          //取消
          .catch(() => {});
      
    },

  },
  created() {
    this.getListsFun();
  },
};
</script>

<style lang="scss" scoped>
.combox {
  .toplogo {
    height: 40px;
    // margin-bottom: 40px;
    padding: 0 13px 0 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    background-color: #fff;
    text-align: center;

    .left {
      width: 7.5%;
      height: 40px;
      line-height: 40px;
      font-size: 28px;
    }
    p {
      justify-content: center;
    }
  }
   .pagebtm {
    height: 50px;
    padding: 0 4px;
    background-color: #fff;
    box-shadow: 0px 0px 12px 1px #ccc;
    display: flex;
    align-items: center;
    position: fixed;
    bottom: 0;
    min-width: 375px;
    max-width: 750px;
    width: 100%;
    .icon-inp {
      width: 13.3%;
      // height: 20px;
      font-size: 20px;
      text-align: center;
    }
    .inp {
      flex: 1;
      border-radius: 15px;
      background-color: #f1f1f1;
      height: 30px;
      margin-right: 2.1%;
      padding: 0 4.5%;
      font-size: 12px;
      color: #999;
      font-weight: bold;
      line-height: 30px;
      outline: none;
      border: 0;
    }
  }
  .usercom {
    margin: 0 13px;
    padding-bottom: 40px;
    .userbox {
      margin: 13px 0;
      .top {
        height: 32px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .right {
          display: flex;
          font-size: 12px;
          align-items: center;
          .like {
            font-size: 20px;
            margin-left: 5px;
          }
        }
      }
      .userimg {
        width: 32px;
        height: 32px;
        border-radius: 50%;
      }
      .btm {
        .reply {
          display: flex;
          font-size: 12px;
          color: #aeaeae;
          .time {
            margin-right: 15px;
          }
          .replybox {
            width: 35px;
            height: 15px;
            padding: 1px;
            text-align: center;
            line-height: 15px;
            border-radius: 5px;
            background-color: #f6f6f6;
            color: #d95546;
          }
        }
        .usercomment {
          font-size: 14px;
        }
      }
      .user {
        font-size: 12px;
        display: flex;
        align-items: center;
      }
    }
    .allcom {
      width: 100%;
      height: 40px;
      background-color: #f1f1f1;
      border-radius: 17px;
      font-size: 14px;
      font-weight: bold;
      text-align: center;
      line-height: 40px;
      color: #636363;
    }
  }
  .icon {
    width: 1em;
    height: 1em;
    vertical-align: -0.15em;
    fill: currentColor;
    overflow: hidden;
  }
}
.combox .box_foot,
.combox .comment {
  margin: 0 13px;
}
.combox .box_foot > .titlebox,
.combox .comment > .titlebox {
  display: flex;
  margin-top: 20px;
  font-size: 15px;
  font-weight: bold;
}
.combox .box_foot > .titlebox > .icon1,
.combox .comment > .titlebox > .icon1 {
  width: 20px;
  height: 20px;
  margin-right: 5px;
}
.combox {
  .van-share-sheet ::v-deep .van-share-sheet__options {
    display: flex;
    justify-content: space-between;
    padding: 16px 0;
  }
  .van-popup {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    // padding: 20px;
    textarea {
      border: 0;
      background-color: #f1f1f1;
      border-radius: 15px;
      padding: 20px;
      width: 80%;
      height: 30%;
      resize: none;
      font-size: 14px;
      font-weight: bold;
    }
    .send {
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 15px;
      font-size: 16px;
      font-weight: bold;
      color: #fff;
      width: 80%;
      padding: 15px 20px;
      margin-top: 20px;
      height: 5%;
      background: rgb(29, 153, 229);
    }
  }
}
</style>